<?php

class Admin_adminController extends Zend_Controller_Action {

    public function init() {
        if (!Zend_Auth::getInstance()->hasIdentity()) {
            return $this->_helper->redirector->goToRoute(array('controller' => 'index'), null, true);
        }
    }

    public function indexAction() {
        $admin1 = Zend_Auth::getInstance()->getIdentity();
        $this->view->admin1 = $admin1;
        $this->_helper->layout->disableLayout();
    }
    
    
     public function administradoresAction() {
         
        $admin1 = Zend_Auth::getInstance()->getIdentity();
        $this->view->admin1 = $admin1;
        $this->_helper->layout->disableLayout();
        $form = new Application_Form_admin();
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {

                $nome = $form->getValue('nome');
                $login = $form->getValue('login');
                $senha = $form->getValue('senha');
                $administradores = new Application_Model_DbTable_admin();
                $administradores->addAdmin($nome, $login, $senha);

                return $this->_helper->redirector('administradores');
            } else {
                $form->populate($formData);
            }
        }
          $administrdores1= new Application_Model_DbTable_admin();
          $this->view->administradores = $administrdores1->listar();
     }
     
     public function editAction() {
        $admin1 = Zend_Auth::getInstance()->getIdentity();
        $this->view->admin1 = $admin1;
        $this->_helper->layout->disableLayout();
        $form = new Application_Form_admin();
        $form->submit->setLabel('Salvar');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('id');
                $nome = $form->getValue('nome');
                $login = $form->getValue('login');
                $senha = $form->getValue('senha');
                $administradores = new Application_Model_DbTable_admin();
                $administradores->edit($id, $nome, $login, $senha);

                $this->_helper->redirector('administradores');
            } else {
                $form->populate($formData);
            }
            } else {
                $id = $this->_getParam('id', 0);
                if ($id > 0) {
                    $administradores2 = new Application_Model_DbTable_admin();
                    $form->populate($administradores2->pegaId($id));
                }
            }
            $administradores1= new Application_Model_DbTable_admin();
            $this->view->administradores = $administradores1->listar();
     }
     
     public function deleteAction() {
         
        $admin1 = Zend_Auth::getInstance()->getIdentity();
        $this->_helper->layout->disableLayout();
        
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Sim') {
                $id = $this->getRequest()->getPost('id');
                $usuario = new Application_Model_DbTable_admin();
                $usuario->delete($id);
            }
            $this->_helper->redirector('administradores');
        } else {
            $id = $this->_getParam('id', 0);
            $administradores1 = new Application_Model_DbTable_admin();
            $this->view->administradores = $administradores1->pegaId($id);
        }
     }

    
    
    public function inscricaoAction() {
        
        // cria uma string randômica através do método direct apenas com letras
        $primaryString = $this->_helper->generateRandom(14, true, false, false);
        // resgata o helper, retornando uma instância do helper
        $helper = $this->_helper->getHelper('generateRandom');
        // ou $helper = $this->_helper->generateRandom;
        // cria um password seguro através de um dos métodos do helper
        $secondString = $helper->hardPassword();
 
        // enviando os dados para view
        $this->view->assign('hardPassword', $secondString);
    
        
        
        $this->_helper->layout->disableLayout();
        $usuario = Zend_Auth::getInstance()->getIdentity();
        $this->view->usuario = $usuario;
        $form = new Application_Form_codigo();
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {

                $nome = $form->getValue('nome');
                $cpf = $form->getValue('cpf');
                $chave = $form->getValue('chave');
                $codigo = new Application_Model_DbTable_codigo();
                $codigo->add_candidato($nome, $cpf, $chave);

                return $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        }
    
    }
}

